// pages/reservation/classconfirm/classconfirm.js
import Toast from '@vant/weapp/toast/toast';
Page({

// 返回上一页
onNavigate(){
  wx.navigateBack({	  
    delta: 1
  })
},

  /**
   * 页面的初始数据
   */
  data: {
    residueNumber:0,
    residuePrice:0,
    price:0,
    cardType:null,
    formData:{},
    radio: '1',
    level2radio:'',
    icon: {
      normal: '/images/radio2.png',
      active: '/images/radio1.png',
    },
    options: [{
        text: '1课时',
      },
      {
        text: '6课时'
      },
      {
        text: '10课时'
      }
    ],
    currentSelection: 0, // 默认选中第一个选项
    "time": [],
    "sort": 0,
    "choseTime": '', //选择的日期
    content:'确定要预约该课程',
    inputValue:'你在哪你咋只能'
  },

setDataInfo(options){
  wx.request({
    url: `http://localhost:8080/keep/teamClass/listToWx?id=${options.id}`,
    header: {
      'Authorization': getApp().globalData.authorization
    },
    success: (resp) => {
      this.setData({
        formData:resp.data.rows[0],
        price:resp.data.rows[0].currentPrice
      })
      console.log(this.data.formData)
    }
})
},



// 立即购买扣去余额
setBalance() {
    if (this.data.radio == 1 || this.data.level2radio == 4) {
      if (this.data.radio == 1) {
        var isCardType = 2
      } else if (this.data.level2radio == 4) {
        var isCardType = null
      }
      
      wx.request({
        url: 'http://localhost:8080/keep/balance',
        data: {
          userId:getApp().globalData.loginfo.userId,
          residuePrice:-this.data.price,
          morePrice:0,
          cardType:isCardType
        },
        method: 'PUT',
        header: {
          'Authorization': getApp().globalData.authorization
        },
        success: resp => {
          if (resp.data.code == 200) {
            Toast.success('购买成功');
          }else{
            Toast.fail('余额不足');
          }
        },
      })
    }else if (this.data.level2radio == 3) {
      this.setBalanceNum()
    }
},


// 立即购买扣去次数余额
setBalanceNum() { 
  wx.request({
    url: 'http://localhost:8080/keep/balanceNum',
    data: {
      userId:getApp().globalData.loginfo.userId,
      residueNumber:-1
    },
    method: 'PUT',
    header: {
      'Authorization': getApp().globalData.authorization
    },
    success: resp => {
      if (resp.data.code == 200) {
        Toast.success('购买成功');
      }else{
        Toast.fail('余额不足');
      }
    },
  })
},

//会员卡
getClubCard(){
  wx.request({
    url: 'http://localhost:8080/keep/balance/userId/'+getApp().globalData.loginfo.userId,
    method:'GET',
    header:{
      'Authorization': getApp().globalData.authorization
    },
    success:resp=>{
      this.setData({
        residuePrice:resp.data.data.residuePrice,
        // isPrice:true
      })
    }

  })

  wx.request({
    url: 'http://localhost:8080/keep/balanceNum/userId/'+getApp().globalData.loginfo.userId,
    method:'GET',
    header:{
      'Authorization': getApp().globalData.authorization
    },
    success:resp=>{
      this.setData({
        residueNumber:resp.data.data.residueNumber,
        // isNumber:true
      })
    }

  })
},



// 提交按钮
gosubmit(){
  this.setBalance()
  // wx.showModal({
  //   title: '提示',
  //   content: this.data.content,
  //   success: function(res) {
     
  //     if (res.confirm) {
  //       wx.showToast({
  //         title: '支付成功',
  //         icon: 'none',
  //         duration: 1000
  //       });
  //     } 
  //   }
  // });
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getClubCard()
    this.setDataInfo(options)
    this.setData({
      time: getDates(7, getCurrentMonthFirst()),
      choseTime: getDates(7, getCurrentMonthFirst())[0].day
  })
  console.log(options)
  },
  // 课时选择后变蓝色
  onOptionTap: function (e) {
    const index = e.currentTarget.dataset.index;
    // 更新当前选中的索引
    this.setData({
      currentSelection: index
    });
  },

  // 预约时间    
  choseTime(e) {
    console.log(e)
    this.setData({
      choseTime: e.currentTarget.dataset.chosetime
    }) 
  },
  // 预约备注输入内容
  input(e) {
    this.setData({
      inputValue: e.detail.value
    }) 
  
  },
  // 支付方式 

  onChange(event) {
    console.log(event)
    this.setData({
      radio: event.detail,
    });
  },
// 会员卡二级单选框
  onChange2(event) {
    this.setData({
      level2radio: event.detail,
    });
  },
  toggleMenu: function() {
    this.setData({
      menuVisible: !this.data.menuVisible
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
//获取当前时间
function getCurrentMonthFirst() {
  var date = new Date();
  var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  return todate;
};

function dateLater(dates, later) {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  dateObj.year = date.getFullYear();
  dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.week = show_day[day];
  return dateObj;
};

function getDates(days, todate = getCurrentMonthFirst()) { //todate默认参数是当前日期，可以传入对应时间
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}